package servlet.product;

import service.impl.UserServiceImpl;
import utils.CookieUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;

@WebServlet("/PLogin.do")
public class LoginServlet extends HttpServlet {
    UserServiceImpl userService = new UserServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession();

        String username = req.getParameter("username");
        String password = req.getParameter("password");
        if (userService.login(username, password) != null) {
            session.setAttribute("username", username);
            session.setAttribute("password", password);
            resp.addCookie(new Cookie("username", username));
            resp.addCookie(new Cookie("password", password));
            resp.sendRedirect("./c.do");
        } else {
            resp.sendRedirect("./login.jsp");
        }


    }
}
